package com.example.wasaimovie.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wasaimovie.Mapper.AdminMapper;
import com.example.wasaimovie.Model.entity.Admin;
import com.example.wasaimovie.Service.AdminService;
import com.example.wasaimovie.Util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 杨健康
* @description 针对表【t_admin(4.2.2 管理员表（t_admin）)】的数据库操作Service实现
* @createDate 2022-05-25 14:34:10
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Autowired
    AdminMapper adminMapper;

    @Value("${movie.image.url.path}")
    String url;

    @Override
    public Map<String, Object> adminLogin(Admin admin) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("username", admin.getUsername());
        List<Admin> list = adminMapper.selectList(qw);
        Map<String, Object> result = new HashMap<>();
        if (!list.isEmpty()){
            // 存在该用户
            Admin admin1 = list.get(0);
            if (admin.getPassword().equals(admin1.getPassword())){
                // 用户名密码相同
                admin1.setAvatar(url + admin1.getAvatar());
                result.put("admin", admin1);
                List<String> roles = new ArrayList<>();
                roles.add("admin");
                String token = JwtTokenUtil.createToken(admin1.getId(), roles, JwtTokenUtil.REMEMBER_EXPIRATION_TIME);
                result.put("token", token);
            }
        }
        return result;
    }
}




